package qeb.handheldmedical.instruction;

import java.util.HashMap;

import org.json.JSONArray;

import qeb.handheldmedical.R;
import qeb.handheldmedical.adapter.InstructionItemAdapter;
import qeb.handheldmedical.adapter.PatientAdapter;
import qeb.handheldmedical.adapter.SlideListView;
import qeb.handheldmedical.dialog.MultiDialog;
import qeb.handheldmedical.helper.WSHelper;
import qeb.handheldmedical.model.InPatient;
import qeb.handheldmedical.patient.InPatientList;
import android.app.Activity;
import android.graphics.Picture;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2016年10月18日 下午2:26:58
 * @declaration 长期医嘱
 */
public class Instruction extends Activity {

	private SlideListView slvcqyz;
	private InPatient pi;
	private JSONArray jsonData;
	private InstructionItemAdapter adapter;
	private Button btn_title_back, btn_title_right;
	private TextView tv_title;
	private MultiDialog mDialog;
	private String yzlx;
	private static final int UPDATE = 0;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.instruction);
		pi = (InPatient) getIntent().getSerializableExtra("patientinfo");
		yzlx = getIntent().getStringExtra("yzlx");
		initUI();
		setTitle();
		loadData();
	}

	private void setTitle() {
		if (yzlx.equals("0")) {
			tv_title.setText("长期医嘱");
		} else {
			tv_title.setText("临时医嘱");
		}
	}

	private void loadData() {
		mDialog = new MultiDialog(Instruction.this,
				MultiDialog.DialogType.WAITING);
		mDialog.setMessage("正在加载，请稍候......");
		mDialog.show();
		new Thread() {

			@Override
			public void run() {
				try {
					getData();
					myHandler.sendEmptyMessage(UPDATE);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}.start();
	}

	Handler myHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UPDATE:
				BandList();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
			// isRunning=false;
			mDialog.cancel();
		}
	};

	/**
	 * 读取数据
	 */
	private void getData() {

		HashMap<String, String> map = new HashMap<String, String>();
		map.put("hiszyxh", pi.HISZYXH);
		map.put("lsyz", yzlx);

		jsonData = WSHelper.getWebserviceFun("GetOrderByHISPatientNo", map);

	}

	/**
	 * 加载UI
	 */
	private void initUI() {
		// TODO Auto-generated method stub
		slvcqyz = (SlideListView) findViewById(R.id.slvcqyz);
		tv_title = (TextView) findViewById(R.id.tv_top_title);
		btn_title_back = (Button) findViewById(R.id.btn_title_back);
		btn_title_back.setOnClickListener(MyOnClickListener);
		btn_title_right = (Button) findViewById(R.id.btn_title_right);
		btn_title_right.setOnClickListener(MyOnClickListener);
	}

	private OnClickListener MyOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_title_back:
				Instruction.this.finish();
				break;
			case R.id.btn_title_right:
				loadData();
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 绑定list
	 */
	private void BandList() {
		// TODO Auto-generated method stub
		if (jsonData != null && jsonData.length() > 0) {

			if (adapter != null) {
				adapter.notifyDataSetChanged();
			}

			adapter = new InstructionItemAdapter(this, jsonData);

			slvcqyz.setAdapter(adapter);
		}
	}
}
